package com.hw.hcit.feature;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hw.hcit.R;
import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;

public class TouchScreenTest extends JTestItem implements OnClickListener {
	private static String TAG = "TouchScreenTest";
	private Context mContext;
	private TouchView mTouchView;
	private boolean mPowerClicked;
    private RelativeLayout relativeLayout;
    private TextView tvTextView;
    private Button mButton1,mButton2;
	
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			/*if(mTouchView.finished){
			 relativeLayout.setVisibility(View.VISIBLE);
			 mTouchView.
			 Log.d("t", "--------->"+"relativeLayout.setVisibility(View.VISIBLE)");
			}*/
			/*15-11-10 gaoshenpan fix not sendMessage when touchfinished begin */
			if(mTouchView.getVisibility()==8){
				relativeLayout.setVisibility(View.VISIBLE);
				Log.d(TAG, "--------->notMesssge");
			}else{
				mHandler.sendEmptyMessageDelayed(0, 1000);
				Log.d(TAG, "--------->sendMessage");
			}
			Log.d(TAG, "--------->"+mTouchView.getVisibility());
			/*15-11-10 gaoshenpan fix not sendMessage when touchfinished end */
		}
	};

	public TouchScreenTest(JCtrlCallback cb) {
		super(cb);
	}

	public TouchScreenTest() {
		super();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.touchscreen_test, container,
				false);
		relativeLayout = (RelativeLayout) rootView.findViewById(R.id.confirm);
		mTouchView = (TouchView) rootView.findViewById(R.id.touchview);
		tvTextView = (TextView)rootView.findViewById(R.id.test_title);
		tvTextView.setText(getString(R.string.touchscreen_test));
		mTouchView.setJTestItem(this);
		mButton1 = (Button) rootView.findViewById(R.id.button1);
		mButton2 = (Button) rootView.findViewById(R.id.button2);
		mButton1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(5, 'P');
					} else {
						rewriteOneByteOfNv2499(36, 'P');
					}
				}
				 testFinish(true, 1, true);
				
			}
		});
		mButton2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(5, 'F');

					} else {
						rewriteOneByteOfNv2499(36, 'F');
					}
				}
				testFinish(false, 1, false);
				
			}
		});
		return rootView;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mContext = activity;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Settings.System.putInt(mContext.getContentResolver(),
				Settings.System.POINTER_LOCATION, 1);
		/*15-10-21 gaoshenpan fix full screen not show navigationbar begin*/
		Settings.Global.putInt(Config.GetActivity().getContentResolver(), "mmi_test_fullscreen", 1);
		/*15-10-21 gaoshenpan fix full screen not show navigationbar end*/
		mTouchView.setSystemUiVisibility();
		mPowerClicked = false;
		mHandler.sendEmptyMessage(0);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		/*15-11-10 gaoshenpan fix not sendMessage when touchfinished begin */
		mHandler.removeMessages(0);
		/*15-11-10 gaoshenpan fix not sendMessage when touchfinished end */
		Settings.System.putInt(mContext.getContentResolver(),
				Settings.System.POINTER_LOCATION, 0);
		/*15-10-21 gaoshenpan  show navigationbar begin*/
		Settings.Global.putInt(Config.GetActivity().getContentResolver(), "mmi_test_fullscreen", 0);
		/*15-10-21 gaoshenpan  show navigationbar end*/
	}

	@Override
	public boolean getResult() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String getResultInfo() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void startTest() {
		// TODO Auto-generated method stub

	}

	@Override
	public void stopTest() {
		// TODO Auto-generated method stub

	}

	public void refresh() {

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}
	 @Override
	    public boolean dispatchKeyEvent(KeyEvent event) {
	        // TODO Auto-generated method stub
	        return true;
	    }
/*
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		// TODO Auto-generated method stub
		if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
			if (mPowerClicked) {
				testFinish(false, 1, false);
				return true;
			}
			showTestUncomplete();
			return true;
		} else if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
			mPowerClicked = true;
			showPowerClicked();
			return true;
		}
		return super.dispatchKeyEvent(event);
	}*/

}
